// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	1/5/2015 11:09:11 PM				
// -----------------------------

#include "TripleOscillator.h"

#include "Oscillator.h"
#include "SineOscillator.h"
#include "TriangleOscillator.h"
#include "SawtoothOscillator.h"
#include "SquareOscillator.h"

#include "SoundSample.h"

#define SEMITONE		1.05f

TripleOscillator::TripleOscillator(SoundWaveType osc1, SoundWaveType osc2, SoundWaveType osc3)
	: m_lpOscillator1(Oscillator::Create(osc1))
	, m_lpOscillator2(Oscillator::Create(osc2))
	, m_lpOscillator3(Oscillator::Create(osc3))
	, Osc1Coarse(0)
	, Osc2Coarse(0)
	, Osc2RelativeVolume(1.0f)
	, Osc2PhaseOffset(0.0f)
	, Osc3Coarse(0)
	, Osc3RelativeVolume(1.0f)
	, Osc3PhaseOffset(0.0f)
{
}

TripleOscillator::~TripleOscillator()
{
	delete m_lpOscillator1;
	delete m_lpOscillator2;
	delete m_lpOscillator3;
}

Float TripleOscillator::Sample(Float frequency, Float amplitude, UInt32 harmonics, Float t) const
{
	// First Oscillator
	m_lpOscillator1->Amplitude = amplitude;
	m_lpOscillator1->Frequency = frequency * SoundSample::CentsToFreqScale(Osc1Coarse);
	m_lpOscillator1->Harmonics = harmonics;
	Float s1 = m_lpOscillator1->Sample(t);

	// Second Oscillator
	m_lpOscillator2->Amplitude = amplitude * Osc2RelativeVolume;
	m_lpOscillator2->Frequency = frequency * SoundSample::CentsToFreqScale(Osc2Coarse);
	m_lpOscillator2->Harmonics = harmonics;
	Float s2 = m_lpOscillator2->Sample(Math::Max(0.0f, t + Osc2PhaseOffset));

	// Third Oscillator
	m_lpOscillator3->Amplitude = amplitude * Osc3RelativeVolume;
	m_lpOscillator3->Frequency = frequency * SoundSample::CentsToFreqScale(Osc3Coarse);
	m_lpOscillator3->Harmonics = harmonics;
	Float s3 = m_lpOscillator3->Sample(Math::Max(0.0f, t + Osc3PhaseOffset));

	return s1 + s2 + s3;
}